package com.mhkj.execute.feedback.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Join;
import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.mhkj.execute.feedback.dao.PlanFeedBackDao;
import com.mhkj.execute.feedback.entity.PlanFeedBack;
import com.mhkj.execute.feedback.service.PlanFeedBackService;
import com.mics.core.common.componet.Pagination;
import com.mics.core.system.base.service.impl.BaseServiceImpl;
import com.mics.core.system.organizational.bean.Employee;
import com.mics.core.system.organizational.service.OrgService;

/**
 * 计划反馈
 */
@Service
public class PlanFeedBackServiceImpl extends BaseServiceImpl<PlanFeedBack> implements PlanFeedBackService {

	@Autowired
	private PlanFeedBackDao planFeedBackDao;
	@Resource
	private OrgService orgService;

	@Autowired
	public void setPlanFeedBackDao(PlanFeedBackDao PlanFeedBackDao) {
		super.setBaseRepository(PlanFeedBackDao);
	}

	/**
	 * 分页查询月度计划反馈
	 */
	@Override
	public Pagination<PlanFeedBack> findPage(PlanFeedBack planFeedBack, Integer pageIndex, Integer pageSize) {
		Specification<PlanFeedBack> spec = new Specification<PlanFeedBack>() {
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 8151653297633251393L;

			@Override
			public Predicate toPredicate(Root<PlanFeedBack> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

	              if(!StringUtils.isEmpty(planFeedBack.getPlanNode())){
	            	  Join<Object, Object> planNode = root.join("planNode", JoinType.LEFT);
	            	  Predicate p2=criteriaBuilder.equal(planNode.get("id"),planFeedBack.getPlanNode().getId());
	                  lstPredicates.add(p2);
	              }

				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		PageRequest pageable = PageRequest.of(pageIndex, pageSize, Sort.Direction.DESC, "createDate","id");
		Page<PlanFeedBack> pageT = planFeedBackDao.findAll(spec, pageable);
		Pagination<PlanFeedBack> pagination = new Pagination<PlanFeedBack>();
		pagination.setTotal(planFeedBackDao.count(spec));

		for(PlanFeedBack planFeedBack2 : pageT){
			if(StringUtils.hasText(planFeedBack2.getCreateBy())){
				Employee manager = orgService.employeeInfo(planFeedBack2.getCreateBy());
				planFeedBack2.setCreateName(manager.getName());
			}
		}
		pagination.setData(pageT.getContent());
		return pagination;
	}

	/**
	 * 根据业务类型查询
	 */
	@Override
	public List<Object[]> getCountByFeedBack(String taskType) {
		return planFeedBackDao.getCountByFeedBack(taskType);
	}

	@Override
	public Pagination<PlanFeedBack> findPageByPlanNodeId(Long planNodeId, Integer pageIndex, Integer pageSize) {
		Specification<PlanFeedBack> spec = new Specification<PlanFeedBack>() {
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 306602690902191635L;

			@Override
			public Predicate toPredicate(Root<PlanFeedBack> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				if (!StringUtils.isEmpty(planNodeId)) {
					Join<Object, Object> planNode = root.join("planNode", JoinType.LEFT);
					lstPredicates.add(criteriaBuilder.equal(planNode.get("id").as(Long.class), planNodeId));
				}

				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		PageRequest pageable = PageRequest.of(pageIndex, pageSize, Sort.Direction.ASC, "id");
		Page<PlanFeedBack> pageT = planFeedBackDao.findAll(spec, pageable);
		Pagination<PlanFeedBack> pagination = new Pagination<PlanFeedBack>();
		pagination.setTotal(planFeedBackDao.count(spec));
		pagination.setData(pageT.getContent());
		return pagination;
	}

	/**
	 * 查询当前计划节点的最新汇报
	 */
	@Override
	public PlanFeedBack findFeedBackPlanNodeId(Long planNodeId) {

		Specification<PlanFeedBack> spec = new Specification<PlanFeedBack>() {
			/**
			 * 序号
			 */
			private static final long serialVersionUID = 306602690902191635L;

			@Override
			public Predicate toPredicate(Root<PlanFeedBack> root, CriteriaQuery<?> query,
					CriteriaBuilder criteriaBuilder) {
				List<Predicate> lstPredicates = new ArrayList<Predicate>();
				Predicate[] arrayPredicates = new Predicate[lstPredicates.size()];

				if (!StringUtils.isEmpty(planNodeId)) {
					Join<Object, Object> planNode = root.join("planNode", JoinType.LEFT);
					lstPredicates.add(criteriaBuilder.equal(planNode.get("id").as(Long.class), planNodeId));
				}

				return criteriaBuilder.and(lstPredicates.toArray(arrayPredicates));
			}
		};
		List<PlanFeedBack> list = planFeedBackDao.findAll(spec);

		PlanFeedBack pfb = new PlanFeedBack();
		for (PlanFeedBack planFeedBack : list) {
			if (StringUtils.isEmpty(pfb.getCurrentProgress())) {
				pfb = planFeedBack;
			} else {
				int cp = pfb.getCurrentProgress();
				int cp1 = planFeedBack.getCurrentProgress();
				if (cp < cp1) {
					pfb = planFeedBack;
				} else if (cp == cp1) {

					long time = pfb.getCreateDate().getTime();
					long time2 = planFeedBack.getCreateDate().getTime();
					if (time < time2) {
						pfb = planFeedBack;
					}
				}
			}
		}
		return pfb;
	}

	@Override
	public String changeByStauts() {
		// 获取列表中所有的状态值
		List<PlanFeedBack> findAll = planFeedBackDao.findAll();
		for (PlanFeedBack planFeedBack : findAll) {
			Integer status = planFeedBack.getStatus();
			if (status == 0) {
				planFeedBack.setStatus(1);
			    super.edit(planFeedBack);
			}
		}
		return "OK";

	}

	@Override
	public List<PlanFeedBack> listCompelete() {
		return planFeedBackDao.listCompelete();
	}

}
